<template>
  <div class="chart" />
</template>

<script>
  import * as echarts from 'echarts'

  import resize from './mixins/resize'

  export default {
    mixins: [resize],
    props: {
      autoResize: {
        type: Boolean,
        default: true
      },
      chartData: {
        type: Object,
        required: true
      }
    },
    data() {
      return {
        chart: null
      }
    },
    watch: {
      chartData: {
        deep: true,
        handler(val) {
          this.setOptions(val)
        }
      }
    },
    mounted() {
      this.$nextTick(() => {
        this.initChart()
      })
    },
    beforeDestroy() {
      if (!this.chart) {
        return
      }
      this.chart.dispose()
      this.chart = null
    },
    methods: {
      initChart() {
        this.chart = echarts.init(this.$el, 'macarons')
        this.setOptions(this.chartData)
      },
      setOptions({
        expectedData,
        actualData
      } = {}) {
        this.chart.setOption({
          xAxis: {

            data: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'],
            boundaryGap: false,
            axisLabel: {
              color: '#43425D',
              fontSize: 9,
            }
          },
          grid: {
            top: 10,
            left: '4%',
            right: '4%',
            bottom: '2%',
            containLabel: true
          },
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'cross'
            },
            padding: [5, 10]
          },
          yAxis: {
            axisLabel: {
              color: '#43425D',
              show: true,
              fontSize: 9,
              formatter: function(value, index) {
                if (value >= 1000) {
                  value = value / 1000 + 'k';
                } else if (value < 1000 && value >= 0) {
                  value = value;
                }
                if (value <= -1000) {
                  value = value / -1000 + 'k';
                } else if (value < 1000) {
                  value = value;
                } else if (value == 0) {
                  value = value;
                }

                return value
              }
            }
          },

          series: [{
              name: 'expected',

              itemStyle: {
                normal: {
                  color: '#54D8FF',
                  lineStyle: {
                    color: '#54D8FF',
                    width: 2
                  },
                  areaStyle: {
                    opacity: 0.8,
                    color: new echarts.graphic.LinearGradient(0, 0, 0, 0.7, [{
                      offset: 0,
                      color: '#54D8FF'
                    }, {
                      offset: 1,
                      color: 'rgba(255,255,251,0)'
                    }])
                  }
                }
              },
              smooth: true,
              type: 'line',
              data: expectedData,
              animationDuration: 2800,
              animationEasing: 'cubicInOut'
            },
            {
              name: 'actual',
              smooth: true,
              type: 'line',
              itemStyle: {
                normal: {
                  color: '#A3A1FB',
                  lineStyle: {
                    color: '#A3A1FB',
                    width: 2
                  },
                  areaStyle: {
                    opacity: 0.8,
                    color: new echarts.graphic.LinearGradient(0, 0, 0, 0.7, [{
                      offset: 0,
                      color: '#A3A1FB'
                    }, {
                      offset: 1,
                      color: 'rgba(255,255,251,0)'
                    }])
                  }
                }
              },
              data: actualData,
              animationDuration: 2800,
              animationEasing: 'quadraticOut'
            }
          ]
        })
      }
    }
  }
</script>
<style scoped>
  .chart {

    height: 300px;
    width: 100%;
  }

</style>
